package com.zhang.boot.core.vo;

import com.zhang.boot.core.constants.ApiConstant;
import com.zhang.boot.core.constants.ErrorEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Result<T> implements Serializable {

    private int code;
    private String message;
    private T data;

    public Result(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public Result(ErrorEnum errorEnum) {
        this.code = errorEnum.getCode();
        this.message = errorEnum.getMessage();
    }

    public Result(ErrorEnum errorEnum, String message) {
        this.code = errorEnum.getCode();
        this.message = message;
    }

    public static <T> Result ok() {
        return Result.builder()
                .code(ApiConstant.ZERO)
                .message(ApiConstant.SUCCESS)
                .build();
    }

    public static <T> Result ok(T t) {
        return Result.builder()
                .code(ApiConstant.ZERO)
                .message(ApiConstant.SUCCESS)
                .data(t)
                .build();
    }
}
